<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RabbitMQ 事务机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            padding: 2px;
            border-radius: 12px;
            display: inline-block;
        }
        
        .highlight-content {
            background: white;
            padding: 1rem 2rem;
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-exchange-alt mr-4"></i>
                RabbitMQ 事务机制
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解消息队列的可靠性保障</p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>原子性保证</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2"></i>
                    <span>一致性维护</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-check-circle mr-2"></i>
                    <span>可靠传递</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="prose max-w-none">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    在 RabbitMQ 中，事务机制用于确保消息的可靠传递和处理，确保操作的一致性和原子性。RabbitMQ 提供了事务机制来处理消息的生产和消费过程中可能出现的错误。生产者可以通过事务机制来保证消息的可靠传递，而消费者则可以通过手动确认机制来确保消息的成功处理。这些机制共同工作，以确保消息在分布式系统中的一致性和可靠性。尽管事务提供了强大的保证，但也可能带来性能开销，因此在使用时需要根据具体需求权衡。
                </p>
            </div>
        </div>

        <!-- Transaction Flow Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gradient">事务流程可视化</h2>
            <div class="mermaid">
                graph LR
                    A[生产者] -->|1. tx.select| B[开始事务]
                    B -->|2. basicPublish| C[发布消息]
                    C -->|3a. tx.commit| D[提交事务]
                    C -->|3b. tx.rollback| E[回滚事务]
                    D --> F[消息持久化]
                    E --> G[消息丢弃]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#27c93f,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#ff5f56,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#fa709a,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1: Transaction Overview -->
        <section class="mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <span class="text-gradient mr-3">1.</span>
                    事务概述
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                    事务是指一组操作要么全部成功，要么全部失败，这样可以保证系统在面对异常情况时保持一致性。在 RabbitMQ 中，事务主要应用于以下几个方面：
                </p>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                        <div class="feature-icon">
                            <i class="fas fa-paper-plane"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">消息发布</h3>
                        <p class="text-gray-700">确保消息被成功发布到队列，避免消息丢失或重复发送。</p>
                    </div>
                    <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                        <div class="feature-icon">
                            <i class="fas fa-cogs"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">消息处理</h3>
                        <p class="text-gray-700">确保消息被消费者成功处理，维护系统的数据一致性。</p>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Section 2: Message Publishing Transaction -->
        <section class="mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <span class="text-gradient mr-3">2.</span>
                    消息发布事务
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-8 indent-8">
                    RabbitMQ 事务机制允许生产者在发布消息时确保消息的可靠性。事务模式下的消息发布流程包括以下几个步骤：
                </p>
                
                <div class="space-y-6">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-12 h-12 bg-gradient-to-br from-purple-500 to-pink-500 rounded-full flex items-center justify-center text-white font-bold mr-4">
                            1
                        </div>
                        <div class="flex-1">
                            <h4 class="text-xl font-bold mb-2">开始事务</h4>
                            <p class="text-gray-700">生产者在发布消息之前调用 <code class="bg-gray-100 px-2 py-1 rounded">tx.select</code> 方法开始一个新的事务。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-12 h-12 bg-gradient-to-br from-blue-500 to-indigo-500 rounded-full flex items-center justify-center text-white font-bold mr-4">
                            2
                        </div>
                        <div class="flex-1">
                            <h4 class="text-xl font-bold mb-2">发布消息</h4>
                            <p class="text-gray-700">生产者发布消息到 RabbitMQ 服务器。在事务模式下，消息发布操作被暂时保留在内存中，并不会立即写入持久化存储。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-12 h-12 bg-gradient-to-br from-green-500 to-teal-500 rounded-full flex items-center justify-center text-white font-bold mr-4">
                            3
                        </div>
                        <div class="flex-1">
                            <h4 class="text-xl font-bold mb-2">提交事务</h4>
                            <p class="text-gray-700">生产者在发布完所有消息后调用 <code class="bg-gray-100 px-2 py-1 rounded">tx.commit</code> 方法提交事务。这时，RabbitMQ 服务器会将消息写入持久化存储，并确认消息已成功发布。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-12 h-12 bg-gradient-to-br from-red-500 to-orange-500 rounded-full flex items-center justify-center text-white font-bold mr-4">
                            4
                        </div>
                        <div class="flex-1">
                            <h4 class="text-xl font-bold mb-2">回滚事务</h4>
                            <p class="text-gray-700">如果在事务期间发生错误，生产者可以调用 <code class="bg-gray-100 px-2 py-1 rounded">tx.rollback</code> 方法回滚事务。这会导致 RabbitMQ 服务器丢弃在事务期间发布的消息，确保消息不会被部分处理。</p>
                        </div>
                    </div>
                </div>
                
                <div class="mt-8">
                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <span class="dot dot-red"></span>
                                <span class="dot dot-yellow"></span>
                                <span class="dot dot-green"></span>
                            </div>
                            <span>Java</span>
                        </div>
                        <pre><code>Channel channel = connection.createChannel();
channel.txSelect(); // 开始事务

try {
    channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
    channel.txCommit(); // 提交事务
} catch (IOException e) {
    channel.txRollback(); // 回滚事务
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Section 3: Message Acknowledgment -->
        <section class="mb-12">
            <div